package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

//controller  掉service   service 掉dao
@RestController
@Slf4j
@RequestMapping("Payment")
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    @Resource
    private PaymentService paymentService;

    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("******插入结果" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功,serverPort"+serverPort, result);
        } else {
            return new CommonResult(500, "插入数据库失败", null);
        }
    }
//http://localhost:8002/Payment/getPaymentById/1
    @GetMapping("/getPaymentById/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("******插入结果" + paymentById+"测试热部署");
        if (paymentById !=null) {
            return new CommonResult(200, "查询据库成功,serverPort"+serverPort, paymentById);
        } else {
            return new CommonResult(500, "查询数据库失败，没有对应id"+id, null);
        }
         
    }
    @GetMapping(value = "/lb")
    public String getPaymentLB(){
        return serverPort;
    }
    /**
     * cloud-consumer-feign-order80
     * <p>
     * 模拟服务端所用的时长超过客户端的等待
     *
     * @return
     */
    @GetMapping("/feign/timeout")
    public String PaymentFeignTimeOut() {
        //单位  为秒
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  serverPort;
    }

}
